package com.atguigu.oss.service;

import cn.hutool.core.util.RandomUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.management.ObjectName;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class OSSServiceImpl implements OSSService {

    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.file.keyid}")
    private String accessKeyId;

    @Value("${aliyun.oss.file.keysecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.file.bucketname}")
    private String bucketName;

    /**
     * 创建存储空间
     */
    public void createBucket() {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.createBucket(bucketName);
        ossClient.shutdown();
    }

    @Override
    public String uploadFile(MultipartFile file) {
        System.out.println(file);
        // File file1 = new File(file.getOriginalFilename());

        // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        // String objectName = "<yourObjectName>";
        String objectName = file.getOriginalFilename();
        System.out.println(file.getContentType());
        String randomFileName = UUID.randomUUID().toString().replaceAll("-", "");
        // String randomString = RandomUtil.randomString(16);
        objectName = randomFileName + "-" + objectName;
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        // String dateString1 = simpleDateFormat.format(new Date());
        String datePath = new DateTime().toString("yyyy/MM/dd");
        objectName = datePath + "/" + objectName;

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（objectName）。
        // String content = "Hello OSS";
        // PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));

        // FileInputStream fis = new FileInputStream("<your local file>");

        PutObjectResult putObjectResult = null;
        try {
            putObjectResult =  ossClient.putObject(bucketName, objectName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        ossClient.shutdown();

        System.out.println(putObjectResult);
        // String uri = putObjectResult.getResponse().getUri();
        // System.out.println(uri);

        // https://atguigu-edu-1001.oss-cn-hangzhou.aliyuncs.com/image/9d3acb4a23fbbe4a517fd405a6488859.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;
    }

    @Override
    public String uploadAvatar(MultipartFile file) {
        String url = uploadFile(file);
        return url;
    }
}
